﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Models;
using WindowManagerService;
using ImageProcessor;
using WindowsInput;
using Exception = System.Exception;
using PokerTable = Models.PokerTable;

namespace BI.ViewModels
{
    public class THM_TableViewModel
    {
        private WindowsManager windowsManager = null;
        private KeyboardService keyboardService = null;
        private PokerWindow pokerWindow = null;
        private ImageManager imageManager = null;
        private Repository repository = null;

        public PokerWindow PokerWindow
        {
            get { return pokerWindow; }
        }

        public THM_TableViewModel(PokerWindow pokerWindow)
        {
            windowsManager = new WindowsManager();
            keyboardService = new KeyboardService(pokerWindow.ID);
            imageManager = new ImageManager();
            repository = new Repository();
            this.pokerWindow = pokerWindow;
        }

        public string SaveScreenOfTableWindow()
        {
            try
            {
                string pathToDirectoy = windowsManager.GetScreenFromWindow(pokerWindow);
                return pathToDirectoy;
            }
            catch (Exception ex)
            {
                DAL.Exception exception = new DAL.Exception() { ExceptionMessage = ex.Message };
                exception.InnerException = ex.InnerException.ToString() ?? string.Empty;
                exception.TimeStamp = DateTime.Now;

                repository.LogException(exception);

                throw ex;
            }
        }

        public PokerTable GetPokerTable(string pathToDirectoy)
        {
            try
            {
                PokerTable pokerTable = imageManager.GetPokerTableFromImage(pathToDirectoy);
                return pokerTable;
            }
            catch (Exception ex)
            {
                DAL.Exception exception = new DAL.Exception() { ExceptionMessage = ex.Message };
                exception.InnerException = ex.InnerException.ToString() ?? string.Empty;
                exception.TimeStamp = DateTime.Now;

                repository.LogException(exception);

                throw ex;
            }
        }

        public void SendCheckCallKey()
        {
            keyboardService.SetFocus();

            InputSimulator.SimulateKeyDown(VirtualKeyCode.CONTROL);
            InputSimulator.SimulateKeyPress(VirtualKeyCode.DOWN);
            InputSimulator.SimulateKeyUp(VirtualKeyCode.CONTROL);
        }

        public void SendFoldCallKey()
        {
            keyboardService.SetFocus();

            InputSimulator.SimulateKeyDown(VirtualKeyCode.CONTROL);
            InputSimulator.SimulateKeyPress(VirtualKeyCode.LEFT);
            InputSimulator.SimulateKeyUp(VirtualKeyCode.CONTROL);
        }

        public void SendBetRaiseCallKey()
        {
            keyboardService.SetFocus();

            InputSimulator.SimulateKeyDown(VirtualKeyCode.CONTROL);
            InputSimulator.SimulateKeyPress(VirtualKeyCode.RIGHT);
            InputSimulator.SimulateKeyUp(VirtualKeyCode.CONTROL);
        }

        public void SendResult(PokerTable pokerTable)
        {
            switch (pokerTable.Action)
            {
                case TableAction.Check:
                    SendCheckCallKey();
                    break;
                case TableAction.Call:
                    SendCheckCallKey();
                    break;
                case TableAction.Fold:
                    SendFoldCallKey();
                    break;
                case TableAction.Bet:
                    SendBetRaiseCallKey();
                    break;
                case TableAction.Raise:
                    SendBetRaiseCallKey();
                    break;
            }

            DAL.PokerTable dalPokerTable = ObjectTranslator.TranlatePokerTable(pokerTable);
            repository.SavePokerTableDecision(dalPokerTable);
        }
    }
}
